<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * M_verificationlink
 *
 * This is an M_resendverificationlink of a few basic verificationlink interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Channels_list extends REST_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('mdl_channels_list');
		
	}

   /* function verificationlink_post()
    {
        //$this->some_model->updateverificationlink( $this->get('id') );
     
		
$data['username']=$this->post('username'); 
$data['password']=$this->post('password'); 
$data['level_id']=$this->post('level_id'); 



		$query = $this->mdl_verificationlink->insert($data);
		
	
		if ($query){
			
			$this->response(array('status' => 'true'),200);
		}else{
			$this->response(array('status' => 'false'));
		}

    }
	*/
	
	function open_channels_list_menu_post(){
		      

		//post
		$this->load->helper('jwt_helper');
		$appsidonapps=$this->post('appsidonapps'); 
		$username=$this->post('username'); 
		$limit=$this->post('limit'); 
		$num=$this->post('num'); 
		
		
		$token = JWT::decode($_POST['token'], 'dompeto');
		$appsidonappstoken= $token->appsidonapps;
        $usernametoken= $token->username;
        	
		$resp = 'Channels_list';
		
		if($username==$usernametoken && $appsidonapps==$appsidonappstoken) {
		
				// cek user channels		
		$arrwhere="username = '" . $username  . "'";
		$query = $this->mdl_channels_list->get_item_user_channels(0, 0, $arrwhere);		
	
			if ($query->num_rows() > 0)
		{
			$i=0;
			foreach($query->result_array() as $row)
			{
				$user_channels_list = (unserialize($row['userchannels_list']));
				$i++;
			}
		
		}
		unset($user_channels_list[0]);
		
		
		$channelstatus = '1';
		$arrwhere="channelstatus = '" . $channelstatus  . "'";
		$query = $this->mdl_channels_list->get_item($limit, $num, $arrwhere);
		
		if ($query->num_rows() > 0)
		{
			$i=0;
			foreach($query->result_array() as $row)
			{
				
				$channels_list[$i]['channelid'] = $row['channelid'];
				$channels_list[$i]['channeltitle'] = $row['channeltitle'];
				$channels_list[$i]['listimage'] = $row['listimage'];
				$channelid_menu = $row['channelid'];
				
				$joinedstatus = 'false';
				$j=0;
		foreach($user_channels_list as $row)
			{
				$channelid_user = $row['channelid'];
				
				if($channelid_menu==$channelid_user) {
				$joinedstatus = 'true';
				}
				
				$j++;
			}
						
				
				
				$channels_list[$i]['joinedstatus'] = $joinedstatus;
				
				
				$i++;
			}
		
		}
		
		
		

		
	
	
	
	
		}
	
	 $this->response($channels_list, 200);		
	
			
	
	}
	
	function open_user_channels_post(){
		      

		//post
		$this->load->helper('jwt_helper');
		$appsidonapps=$this->post('appsidonapps'); 
		$username=$this->post('username'); 
		$token = JWT::decode($_POST['token'], 'dompeto');
		$appsidonappstoken= $token->appsidonapps;
        $usernametoken= $token->username;
        	
		$resp = 'Channels_list';
		
		if($username==$usernametoken && $appsidonapps==$appsidonappstoken) {
		
		
		// cek user channels		
		$arrwhere="username = '" . $username  . "'";
		$query = $this->mdl_channels_list->get_item_user_channels(0, 0, $arrwhere);		
	
			if ($query->num_rows() > 0)
		{
			$i=0;
			foreach($query->result_array() as $row)
			{
				
				$user_channels_list = (unserialize($row['userchannels']));
				
				
					
				$i++;
			}
		
		}
		
		


	
		}
		$user_channels =$user_channels_list;
	$channels_list[]=$user_channels;
	 $this->response($channels_list, 200);		
	
			
	
	}
	
	function join_channel_post(){
		      

		//post
		$this->load->helper('jwt_helper');
		$appsidonapps=$this->post('appsidonapps'); 
		$username=$this->post('username'); 
		$channelid=$this->post('channelid'); 
		$token = JWT::decode($_POST['token'], 'dompeto');
		$appsidonappstoken= $token->appsidonapps;
        $usernametoken= $token->username;
        	
		$resp = 'join channel';
		
		if($username==$usernametoken && $appsidonapps==$appsidonappstoken) {
		
		
		
		
		// cek user channels		
		$arrwhere="username = '" . $username  . "'";
		$query = $this->mdl_channels_list->get_item_user_channels_list(0, 0, $arrwhere);
		$result = $query->result_array();
		$userchannels_listx = (unserialize($result[0]['userchannels_list']));
		
	
				$userchannels_listx[]= array(
					'channelid'=> $channelid,
					'cardholdername'=> '',
					'membersince'=> '',
					'validuntil'=> '',
					'cardnumber'=> ''
					);
					
			
		
		
		}
		
		$data['userchannels_list']=serialize($userchannels_listx);
		$query = $this->mdl_channels_list->updateuserchannels_list($username,$data);
		
	 $this->response($userchannels_listx, 200);		
	
			
	
	}
	function open_user_channels_list_post(){
		      

		//post
		$this->load->helper('jwt_helper');
		$appsidonapps=$this->post('appsidonapps'); 
		$username=$this->post('username'); 
		$token = JWT::decode($_POST['token'], 'dompeto');
		$appsidonappstoken= $token->appsidonapps;
        $usernametoken= $token->username;
        	
		$resp = 'Channels_list';
		
		if($username==$usernametoken && $appsidonapps==$appsidonappstoken) {
		
		// cek user channels		
		$arrwhere="username = '" . $username  . "'";
		$query = $this->mdl_channels_list->get_item_user_channels_list(0, 0, $arrwhere);		
	
			if ($query->num_rows() > 0)
		{
			$i=0;
			foreach($query->result_array() as $row)
			{
				$userchannels_list = (unserialize($row['userchannels_list']));
				$i++;
			}
		
		}
		
		$channelsize = $userchannels_list[0]['channelsize'];
		$channelsize = sizeof($userchannels_list);
		$i=1;
		for($i=1;$i<=$channelsize-1;$i++) {
		$channelid = $userchannels_list[$i]['channelid'];
		
		// cek user channels		
		$arrwhere="channelid = '" . $channelid  . "'";
		$query = $this->mdl_channels_list->get_item_channels_profile(0, 0, $arrwhere);		
		$result =$query->result_array();
				$channel_profile[$i-1]['channelid'] = $result[0]['channelid'];
				$channel_profile[$i-1]['channeltitle'] = $result[0]['channeltitle'];
				$channel_profile[$i-1]['listimage'] = $result[0]['listimage'];
				
				$channelstatus = 'false';
				if($result[0]['channelstatus']=='1'){
				$channelstatus = 'true';
				};
				$channel_profile[$i-1]['channelstatus'] = $channelstatus;
				$channel_profile[$i-1]['joinedstatus'] = 'true';
	
		
		}


	
		}
	
	 $this->response($channel_profile, 200);		
	
			
	
	}
	
	function open_user_channels_detail_post(){
		      

		//post
		$this->load->helper('jwt_helper');
		$appsidonapps=$this->post('appsidonapps'); 
		$username=$this->post('username'); 
		$channelid=$this->post('channelid'); 
		$token = JWT::decode($_POST['token'], 'dompeto');
		$appsidonappstoken= $token->appsidonapps;
        $usernametoken= $token->username;
        	
		$resp = 'Channels_list';
		$cardholdername = 'false';
				$membersince= 'false';
				$validuntil= 'false';
				$cardnumber= 'false';
				$qrcode= 'false';
				
		if($username==$usernametoken && $appsidonapps==$appsidonappstoken) {
		
		// cek user channels		
		$arrwhere="username = '" . $username  . "'";
		$query = $this->mdl_channels_list->get_item_user_channels_list(0, 0, $arrwhere);		
		$row = $query->result_array();
		$userchannels_list = (unserialize($row[0]['userchannels_list']));
		$channelsize = $userchannels_list[0]['channelsize'];
		
		$i=1;
		for($i=1;$i<=$channelsize;$i++) {
		$userchannelid = $userchannels_list[$i]['channelid'];
		
		if($userchannelid==$channelid)
				
				{
				$cardholdername = $userchannels_list[$i]['cardholdername'];
				$membersince= $userchannels_list[$i]['membersince'];
				$validuntil= $userchannels_list[$i]['validuntil'];
				$cardnumber= $userchannels_list[$i]['cardnumber'];
				$qrcode= $userchannels_list[$i]['qrcode'];
				
				}
		}
		
		
		// cek user channels		
		$arrwhere="channelid = '" . $channelid  . "'";
		$query = $this->mdl_channels_list->get_item_channels_profile(0, 0, $arrwhere);		
		$result =$query->result_array();
		
		$channelstatus = 'false';
				if($result[0]['channelstatus']=='1'){
				$channelstatus = 'true';
		
	$channel_profile[] = array(
	'channelid' => $result[0]['channelid'],
				'channeltitle' => $result[0]['channeltitle'],
				'listimage'=> $result[0]['listimage'],
				'simage'=> $result[0]['simage'],
				'limage'=> $result[0]['limage'],
				'channelstatus'=> $channelstatus,
				'joinedstatus' => 'true',
				'cardholdername' => $cardholdername,
				"membersince"=>$membersince,
				"validuntil"=>$validuntil,
				"cardnumber"=>$cardnumber,
				"qrcode"=>$qrcode
				
				);
				
				
				
				};
				
	
		
	


	
		}
	
	 $this->response($channel_profile, 200);		
	
			
	
	}
    /*
    function verificationlink_delete()
    {
		
    	//$this->some_model->deletesomething( $this->get('id') );$this->_get('id')
        $message = array('message' => 'DELETED!', 'id'=>$this->delete('id'));
		
		$query = $this->mdl_verificationlink->delete($this->delete('id'));
		//$this->response($message,200);
		//$query=1;
		if ($query){
			$this->response(array('status' => 1),200);
		
		}else{
			$this->response(array('status' => 0));
		}
      
    } */
	/*
	 function verificationlink_put()
    {
		
    	//$this->some_model->deletesomething( $this->get('id') );$this->_get('id')
        //$message = array('message' => 'UPDATE!', 'userid'=>$this->delete('userid'), 'name'=>$this->delete('name'));
		$data['userid']=$this->put('userid'); 
$data['username']=$this->put('username'); 
$data['password']=$this->put('password'); 
$data['level_id']=$this->put('level_id'); 

		$query = $this->mdl_verificationlink->update($data['userid'],$data);
		//$this->response($message,200);
		
		if ($query){
			$this->response(array('status' => 1),200);
		
		}else{
			$this->response(array('status' => 0));
		}
      
    } */
    /*
    function verificationlinks_get()
    {
        //$verificationlinks = $this->some_model->getSomething( $this->get('limit') );
        $verificationlinks = array(
			array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com'),
			array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com'),
			3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => array('hobbies' => array('fartings', 'bikes'))),
		);
        
        if($verificationlinks)
        {
            $this->response($verificationlinks, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Couldn\'t find any items!'), 404);
        }
    } */
/*

	public function send_post()
	{
		var_dump($this->request->body);
	}
*/
/*
	public function send_put()
	{
		var_dump($this->put('foo'));
	}
	*/
}